package com.china08.yxyapi.api.manage.stu.mystudy.tifenbao.studyrecord;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.huixue.YxyHuixuePlan;
import com.china08.yxyapi.entity.mg.huixue.YxyHuixuePlanKnow;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.huixue.YxyHuixuePlanKnowTrainQuesRepository;
import com.china08.yxyapi.repository.mg.huixue.YxyHuixuePlanRepository;
import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;

/**
 * 学习记录-列表-右侧
 * 
 */
@CrossOrigin
@RestController
public class TifenbaoStudyRecoredSrhController extends YxyApiBaseController {
    @Autowired
    private MongoTemplate mTemplate;

    @Autowired
    private YxyHuixuePlanRepository yxyHuixuePlanRepository;

    @Autowired
    private YxyHuixuePlanKnowTrainQuesRepository yxyHuixuePlanKnowTrainQuesRepository;

    @RequestMapping(value = "/api_yw/manage/stu/mystudy/tifenbao/studyrecord/recored/srh", method = RequestMethod.POST)
    public RespModel precess(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel)
            throws ParseException {
        DecimalFormat df = new DecimalFormat("######0.0");
        YxyHuixuePlan yxyHuixuePlan = yxyHuixuePlanRepository.findById(reqModel.planId);
        RespModel model = new RespModel();
        model.planId = yxyHuixuePlan.getId();
        // 开始时间
        Integer beginDate = yxyHuixuePlan.getBeginDate();
        String beginDateString = null;
        if (beginDate != null && beginDate != 0) {
            beginDateString = String.valueOf(beginDate);
            beginDateString = beginDateString.substring(0, 4) + "-" + beginDateString.substring(4, 6) + "-"
                    + beginDateString.substring(6);
        }
        // 结束时间
        Integer endDate = yxyHuixuePlan.getEndDate();
        String endDateString = null;
        if (endDate != null && endDate != 0) {
            endDateString = String.valueOf(endDate);
            endDateString = endDateString.substring(0, 4) + "-" + endDateString.substring(4, 6) + "-"
                    + endDateString.substring(6);
        }
        model.beginDate = beginDateString;
        model.endDate = endDateString;
        double expect = yxyHuixuePlan.getLastScore() + yxyHuixuePlan.getExpectScore();
        model.status = yxyHuixuePlan.getCurrentScore() >= expect ? 0 : 1;
        model.progress = yxyHuixuePlan.getComplete();

        model.score = yxyHuixuePlan.getCurrentScore();
        // 计算超过百分多少学生
        int total = yxyHuixuePlanRepository
                .countByBookContentIdAndStatusAndCurrentScoreGreaterThan(yxyHuixuePlan.getBookContentId(), 0, -1);
        if (total == 0) {
            model.scoreProgress = 99d;
        } else {
            int lesstotal = yxyHuixuePlanRepository.countByBookContentIdAndStatusAndCurrentScoreLessThan(
                    yxyHuixuePlan.getBookContentId(), 0, model.score);
            Double aLong = (double) lesstotal / total;
            model.scoreProgress = Double.valueOf(df.format(aLong * 100));
        }
        // 答题总数
        int questotal = yxyHuixuePlanKnowTrainQuesRepository.countByPlanId(model.planId);
        model.count = questotal;
        // 答对个数
        int righttotal = yxyHuixuePlanKnowTrainQuesRepository.countByPlanIdAndPassed(model.planId, 1);
        // 计算答题正确率
        Double rate = (double) righttotal / questotal;
        model.rightProgress = Double.valueOf(df.format(rate * 100));
        // 涉及小节数量
        DBCollection collection3 = mTemplate.getCollection(mTemplate.getCollectionName(YxyHuixuePlanKnow.class));
        DBObject dbObject3 = new BasicDBObject("plan_id", yxyHuixuePlan.getId());
        @SuppressWarnings("rawtypes")
        List jielist = collection3.distinct("book_contents_id", dbObject3);
        model.contentCount = jielist.size();
        return model;

    }

    protected static class RespModel extends YxyApiRespModel {
        // 学习计划id
        public String planId;
        // 开始时间
        public String beginDate;
        // 结束时间
        public String endDate;
        // 完成度
        public Double progress;
        // 学习计划的状态：0--计划完成 , 1--计划失败
        public Integer status;
        // 得分
        public Double score;
        // 分数超过多少学生百分比
        public Double scoreProgress;
        // 答题数量
        public Integer count;
        // 答题正确率
        public Double rightProgress;
        // 涉及小节数量
        public Integer contentCount;
    }

    protected static class ReqModel extends YxyApiPageReqModel {
        // 计划id
        public String planId;
    }
}
